package com.aj.easylibrary.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.aj.easylibrary.R

class EasyActionBarView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
    var txtTitle: String? = null
    var textViewLeftTxt: TextView? = null
    var imageViewLeftIcon: ImageView? = null
    var textViewRightTxt: TextView? = null
    var imageViewRightIcon: ImageView? = null
    var textViewTitle: TextView? = null
    private var mActionBarListener: ActionBarListener? = null
    var leftIconResId: Int = 0
    var rightIconResId: Int = 0
    var rightTxt: String? = null
    var leftTxt: String? = null
    private val NONE = -1
    var isShowLeftIcon: Boolean = false
    var isShowRightIcon: Boolean = false
    var isShowLeftTxt: Boolean = false
    var isShowRightTxt: Boolean = false
    private var mRightTxtColor = -1
    private var mLeftTxtColor = -1
    private var mTitleColor = -1
    private var mBackgroundColor = -1
    private var mRightTxtBackground = -1

    private var mRightTxtFontSize = -1
    private var mLeftTxtFontSize = -1
    private var mTitleTxtSize = -1
    private var mLlMain: LinearLayout? = null

    init {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet) {
        // Load attributes
        val a = context.obtainStyledAttributes(
                attrs, R.styleable.EasyActionBarView)
        txtTitle = a.getString(
                R.styleable.EasyActionBarView_txtTitle)
        mBackgroundColor = a.getColor(
                R.styleable.EasyActionBarView_backgroundColor,
                mBackgroundColor)

        mBackgroundColor = a.getColor(
                R.styleable.EasyActionBarView_backgroundColor,
                mBackgroundColor)
        mRightTxtColor = a.getColor(R.styleable.EasyActionBarView_rightTxtColor, mRightTxtColor)
        mRightTxtBackground = a.getResourceId(R.styleable.EasyActionBarView_rightTxtBackground, -1)

        mLeftTxtColor = a.getColor(R.styleable.EasyActionBarView_leftTxtColor, mLeftTxtColor)
        mTitleColor = a.getColor(R.styleable.EasyActionBarView_titleColor, mTitleColor)
        mRightTxtFontSize = a.getDimensionPixelSize(R.styleable.EasyActionBarView_rightTxtFontSize, mRightTxtFontSize)

        mLeftTxtFontSize = a.getDimensionPixelSize(R.styleable.EasyActionBarView_leftTxtFontSize, mLeftTxtFontSize)

        mTitleTxtSize = a.getDimensionPixelSize(R.styleable.EasyActionBarView_titleTxtFontSize, mTitleTxtSize)

        leftIconResId = a.getResourceId(R.styleable.EasyActionBarView_leftIcon, -1)
        rightIconResId = a.getResourceId(R.styleable.EasyActionBarView_rightIcon, -1)
        rightTxt = a.getString(R.styleable.EasyActionBarView_rightTxt)
        leftTxt = a.getString(R.styleable.EasyActionBarView_leftTxt)

        isShowLeftIcon = a.getBoolean(R.styleable.EasyActionBarView_showLeftIcon, true)
        isShowRightIcon = a.getBoolean(R.styleable.EasyActionBarView_showRightIcon, false)
        isShowLeftTxt = a.getBoolean(R.styleable.EasyActionBarView_showLeftTxt, false)
        isShowRightTxt = a.getBoolean(R.styleable.EasyActionBarView_showRightTxt, false)

        a.recycle()
        initIView(context)


    }

    private fun initIShow() {
        if (isShowLeftIcon) {
            imageViewLeftIcon?.visibility = View.VISIBLE
        }
        if (isShowRightIcon) {
            imageViewRightIcon?.visibility = View.VISIBLE
        }
        if (isShowLeftTxt) {
            textViewLeftTxt?.visibility = View.VISIBLE
        }
        if (isShowRightTxt) {
            textViewRightTxt?.visibility = View.VISIBLE
        }
    }

    private fun initIData() {
        if (!TextUtils.isEmpty(txtTitle)) {
            textViewTitle?.text = txtTitle
        }
        if (mTitleTxtSize != -1) {
            textViewTitle?.paint?.textSize = mTitleTxtSize.toFloat()
        }
        if (mRightTxtFontSize != -1) {
            textViewRightTxt?.paint?.textSize = mRightTxtFontSize.toFloat()
        }
        if (mLeftTxtFontSize != -1) {
            textViewLeftTxt?.paint?.textSize = mLeftTxtFontSize.toFloat()
        }
        if (mRightTxtBackground != -1) {
            textViewRightTxt?.setBackgroundResource(mRightTxtBackground)
        }
        if (mTitleColor != -1) {
            textViewTitle?.setTextColor(mTitleColor)
        }
        if ( mRightTxtColor != -1) {
            textViewRightTxt?.setTextColor(mRightTxtColor)
        }
        if (mLeftTxtColor != -1) {
            textViewLeftTxt?.setTextColor(mLeftTxtColor)
        }

        if (mBackgroundColor != -1) {
            mLlMain?.setBackgroundColor(mBackgroundColor)
        }


        if (!TextUtils.isEmpty(leftTxt)) {
            textViewLeftTxt?.text = leftTxt
        }

        if (!TextUtils.isEmpty(rightTxt)) {
            textViewRightTxt?.text = rightTxt
        }

        if (leftIconResId != NONE) {
            imageViewLeftIcon?.setImageResource(leftIconResId)
        }

        if (rightIconResId != NONE) {
            imageViewRightIcon?.setImageResource(rightIconResId)
        }

    }

    private fun initIEvent() {
        textViewLeftTxt?.setOnClickListener { view ->
            view.isEnabled = false
            mActionBarListener?.onClickLeft(view)
            view.isEnabled = true
        }

        imageViewLeftIcon?.setOnClickListener { view ->
            view.isEnabled = false
            mActionBarListener?.onClickLeft(view)
            view.isEnabled = true
        }

        textViewRightTxt?.setOnClickListener { view ->
            view.isEnabled = false
            mActionBarListener?.onClickRight(view)
            view.isEnabled = true
        }

        imageViewRightIcon?.setOnClickListener { view ->
            view.isEnabled = false
            mActionBarListener?.onClickRight(view)
            view.isEnabled = true
        }
    }


    private fun initIView(context: Context) {
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val rootView = inflater.inflate(R.layout.view_actionbar, this)
        textViewLeftTxt = rootView.findViewById(R.id.tv_left_txt) as TextView
        imageViewLeftIcon = rootView.findViewById(R.id.iv_letf_icon) as ImageView
        textViewRightTxt = rootView.findViewById(R.id.tv_right_txt) as TextView
        imageViewRightIcon = findViewById(R.id.iv_right_icon) as ImageView
        textViewTitle = findViewById(R.id.tv_title) as TextView
        mLlMain = findViewById(R.id.ll_main) as LinearLayout
        initIEvent()
        initIData()
        initIShow()

    }

    fun setActionBarListener(eventListener: ActionBarListener) {
        mActionBarListener = eventListener
    }

    interface ActionBarListener {
        fun onClickLeft(view: View)

        fun onClickRight(view: View)
    }

    fun getBackgroundColor(): Int {
        return mBackgroundColor
    }

    override fun setBackgroundColor(backgroundColor: Int) {
        mBackgroundColor = backgroundColor
    }


}
